package examples.mail;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.lang.StringUtils;

public class MainNml {

	BlockingQueue<String> bq = new LinkedBlockingQueue<String>();
	Random rand = new Random();

	Map<String, List<String>> m = new HashMap<String, List<String>>();

	QueryRunner qr = Db.getQueryRunner();

	class SendThread extends Thread {
		public void run() {
			while (true) {
				String to = null;
				try {
					to = bq.take();
					System.out.println(to);
					Pattern regex = Pattern.compile("[a-zA-Z0-9_-]+@\\w+\\.[a-z]+(\\.[a-z]+)?");
					Matcher matcher = regex.matcher(to);
					if (!matcher.matches()) {
						qr.update("update acc_csdn set stat='mail.add.error'  where too=? ", to);
						continue;
					}

					String hostName = to.split("@")[1];

				 	 if (hostName.equals("qq.com")) {
						continue;
					}  

					List<String> mxs = null;

					if (m.containsKey(hostName)) {
						mxs = m.get(hostName);

					} else {
						mxs = DnsResolver.getMXRecordsForHost(hostName);
						m.put(hostName, mxs);
					}

					if (mxs == null) {
						qr.update("update acc_csdn set stat='nomx'  where too=? ", to);
						continue;
					}
					int r =Math.abs( rand.nextInt() ) ;

					String toserver = mxs.get(r % mxs.size());

					String mailserver = "lyou09-15.yuwen.com";
					String from = "service_15@lyou09-15.yuwen.com";
					String subject = "XXXXXXXXXXXXXXXXXXXX!";
					String htmlcontent = "<A href=\"http://s.click.taobao.com/t_8?e=3D7HZ6jHSTbIWchi2irVZeSulXVLyPJjWL=FDBQXHNg66lBjg%3D%3D&amp;p=3Dmm_28188379_0_0\"><IMG src=\"http://boluozhiwen.svfree.net/up.html\"></A>";
					htmlcontent += Util.randomNumberString(2000);
					String  textcontent=Util.randomNumberString(2000);

					String mail = MailBuild.build(mailserver, from, to, subject, htmlcontent,textcontent);

					SMTPMail.send(from, to, toserver, mailserver, mail);

					qr.update("update acc_csdn set stat='1'  where too=? ", to);

				} catch (Exception e) {
					e.printStackTrace();
					try {
						qr.update("update acc_csdn set stat=?  where too=? ", e.getMessage(), to);
					} catch (SQLException e1) {
						e1.printStackTrace();
					}
				}

			}
		}
	}

	public void start() throws Exception {
		String sql = "select too from acc_csdn where stat='0'    LIMIT 1  ";
		
		sql=" select * from acc_csdn where  too='yangelhun@126.com' ";
		
		List<Object> tos = qr.query(sql, new ColumnListHandler("too"));
		for (Object to1 : tos) {
			String to = (String) to1;
			bq.add(to);
		}
		
	 	bq.clear();
		bq.add("yangelhun@126.com"); 
		
		
		for (int i = 0; i < 10; i++) {
			new SendThread().start();
		}

	}

	public static void main(String[] args) throws Exception {
		new MainNml().start();
	}
}
